package com.dtspread.apps.houseloan.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

import com.dtspread.libs.share.ShareController;
import com.dtspread.libs.share.cfg.PlatformCfg;
import com.dtspread.libs.share.platform.SharePlatform;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.vanchu.libs.common.util.SwitchLogger;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

	private final String LOG_TAG = "WXEntryActivity";
	private IWXAPI _api;

	// private static final int LOGIN_ERR_MALE = 51;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String WX_APP_ID = PlatformCfg.WX_APP_ID;
		_api = WXAPIFactory.createWXAPI(this, WX_APP_ID, false);
		_api.handleIntent(getIntent(), this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.wxentry, menu);
		return true;
	}

	@Override
	public void onReq(BaseReq req) {
		SwitchLogger.d("WXEntryActivity", "onReq transaction:"
				+ req.transaction);
	}

	@Override
	public void onResp(BaseResp resp) {
		SwitchLogger.d(LOG_TAG, "onResp errCode:" + resp.errCode + " errStr:"
				+ resp.errStr + " transaction:" + resp.transaction
				+ "，resp.getType()：" + resp.getType());
		/*if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
			String code = null;
			if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
				SendAuth.Resp sendResp = (SendAuth.Resp) resp;
				code = sendResp.code;
			}
//			gotoLoginIndexActivity(code);
			finish();
			return;
		}*/
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			ShareController.shareSucc(SharePlatform.WEIXIN);
			SwitchLogger.d(LOG_TAG, "----------weixin share succ"); // 微信分享成功
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			ShareController.shareFail(SharePlatform.WEIXIN);
			SwitchLogger.d(LOG_TAG, "----------weixin share cancel"); // 微信分享取消
			break;
		default:
			SwitchLogger.d(LOG_TAG, "----------weixin share fail"); // 微信分享失败
			break;
		}
		finish();
	}

	/*private void gotoLoginIndexActivity(String wxCode) {
		SharedPerferencesUtils.initPerferencesUtils(this).setIsLoginSucc(true);
		Intent intent = new Intent(this, LoginIndexActivity.class);
		intent.putExtra(LoginIndexActivity.LOGIN_WITH_WECHAT_AFTER_CODE_KEY,
				wxCode);
		this.startActivity(intent);
	}*/
}
